﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.BackgroundTransfer;
using System.IO.IsolatedStorage;
using Microsoft.Phone.Tasks;
using OnlineVideos.Library;
using Common.Library;
using OnlineVideos.Data;
using OnlineVideos.Entities;
using OnlineVideos.UI;

namespace OnlineVideos
{
    public partial class Downloding : PhoneApplicationPage
    {
        List<DownloadStatus> download = new List<DownloadStatus>();
        IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
        //OnlineVideoDataContext context = new OnlineVideoDataContext(Constants.DatabaseConnectionString);

        string downloadurl = "";
        public Downloding()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (txttitle.Text != "")
                {
                    // txturl.Text = "http://www.dailymotion.com/cdn/H264-320x240/video/x10v158.mp4?auth=1371363791-9c378a2b3fde6f4fbc5151aa842ebfcf";
                    DownloadManager.SaveDownloadingInfo(txttitle.Text, txturl.Text);
                    AppSettings.DownloadTitle = txttitle.Text;
                    if (BackgroundTransferService.Requests.Count() >= 5)
                    {

                        MessageBox.Show("The maximum number of background file transfer requests for this application has been exceeded. ");
                        return;
                    }

                    string transferFileName = txturl.Text;

                    // string transferFileName = "http://lartsoft.org/ActivityTrakerHelpVideos/Howtoviewtimespentchart.wmv";
                    //Uri transferUri = new Uri(Uri.EscapeUriString(transferFileName), UriKind.RelativeOrAbsolute);
                    //BackgroundTransferRequest transferRequest = new BackgroundTransferRequest(transferUri);
                    //transferRequest.Method = "GET";
                    //string downloadFile = transferFileName.Substring(transferFileName.LastIndexOf("/") + 1);
                    //AppSettings.DownloadFile = downloadFile;

                    //Uri downloadUri = new Uri("shared/transfers/" + downloadFile, UriKind.RelativeOrAbsolute);
                    //transferRequest.DownloadLocation = downloadUri;
                    //string filepath = downloadUri.ToString();
                    //transferRequest.Tag = downloadFile;
                    //DownloadManager.DownloadingStatus(filepath, "", transferRequest.TotalBytesToReceive);
                    //DownloadManager.DeleteDownloadUrls(filepath);
                    if (ResourceHelper.AppName == Apps.Web_Media.ToString())
                        NavigationService.Navigate(new Uri("/Views/DownloadList.xaml", UriKind.Relative));
                    else
                    NavigationService.Navigate(new Uri("/Views/ShowList.xaml", UriKind.Relative));
                    try
                    {
                        //BackgroundTransferService.Add(transferRequest);
                    }
                    catch (InvalidOperationException ex)
                    {
                        MessageBox.Show("Unable to add background transfer request. " + ex.Message);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Unable to add background transfer request.");
                    }
                }
                else
                {
                    MessageBox.Show("please Enter Title");
                }
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in button1_Click event In downloadingpage file.", ex);
            }
        }
        private void imgTitle_MouseEnter(object sender, MouseEventArgs e)
        {
            NavigationService.Navigate(NavigationHelper.MainPanoramaPage);
        }
        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            string url = "";
            if (NavigationContext.QueryString.TryGetValue("url", out url))
            {
                downloadurl = url;
                txturl.Text = downloadurl;
            }
        }
        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            try
            {
                FlurryWP8SDK.Api.EndTimedEvent("Downloding Page");
            }
            catch (Exception ex)
            {

                Exceptions.SaveOrSendExceptions("Exception in OnNavigatedFrom Method In Downloding.cs file.", ex);
            }
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            try
            {
                FlurryWP8SDK.Api.LogPageView();
            }
            catch (Exception ex)
            {

                Exceptions.SaveOrSendExceptions("Exception in OnNavigatedTo Method In Downloding.cs file.", ex);
            }
        }

    }
}